package com.pig4cloud.pig.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.pig4cloud.pig.admin.api.dto.loginDTO;
import com.pig4cloud.pig.admin.api.util.FeignClientRequestInterceptor;
import com.pig4cloud.pig.admin.api.util.HttpTool;
import com.pig4cloud.pig.common.core.constant.SecurityConstants;
import com.pig4cloud.pig.common.security.annotation.Inner;
import feign.RequestTemplate;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import org.bouncycastle.math.Primes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.Base64;
import java.util.Map;

@Controller
@RequestMapping("/login")
@RequiredArgsConstructor
@ConditionalOnProperty(name = "login.login", matchIfMissing = true)
public class loginController {
	@Autowired
	RestTemplate template;
	@Autowired
	LoadBalancerClient loadBalancerClient;

	@Inner(value = false)
	@GetMapping("/login")
	public ModelAndView show() {
		ModelAndView model = new ModelAndView("/login/login");
		model.addObject("ke", "thanks,pig4cloud");
		return model;
	}

	@Inner(value = false)
	@RequestMapping("/toLogin")
	public ModelAndView login(loginDTO param, HttpServletResponse response) {
		ModelAndView model = new ModelAndView();
		//ServiceInstance instance = loadBalancerClient.choose("pig-auth");
		String urls="http://127.0.0.1:9999/auth/oauth2/token?";
		try {
			ResponseEntity<Map> map = template.exchange(urls, HttpMethod.POST, FeignClientRequestInterceptor.httpHeader(param), Map.class);
			model.addObject("id", map.getBody().get("id"));
			model.addObject("userName", map.getBody().get("username"));
			model.setViewName("redirect:/login/index");
		}catch (Exception e) {
			e.printStackTrace();
			model.addObject("loginMsg", "用户密码错误");
			model.setViewName("/login/login");
		}
		return model;
	}


	//@RequestMapping(value = "/index",headers = {"Content-Type: multipart/form-data", "Authorization=Bearer c1c1dd32-767d-44d7-ba8d-6d2790804f77"})
	//@RequestMapping("/index")
	@PostMapping(value = "/index",headers = { "Authorization=Bearer 32965e20-9eb6-42c5-b826-171f50f80a53"})
	public ModelAndView toIndex(HttpServletResponse response) {
		ModelAndView model = new ModelAndView();
		model.setViewName("/login/index");

		return model;
	}
}
